import { lastTestVsiionResult } from "../config/data";
import { indexToVisionComany } from "./common";


/**
 * 视力对象转表单数据
 * @param {object} data 
 * @returns 
 */
const visionResultToData = (data)=>{
    if(data==null){
        return null
    }

    return {
        left_eye:{
            distance:data.distance,
            vision:indexToVisionComany(data.leftCompanyIndex),
            correct_num:data.leftCorrectNumber
        },
        right_eye:{
            distance:data.distance,
            vision:indexToVisionComany(data.rightCompanyIndex),
            correct_num:data.rightCorectNumber
        }
    }
}

/**
 * 散光数据转表单数据
 * @param {object} data 
 * @returns 
 */
const astigmatismResultToData = (data)=>{
    if(!data){
        return {};
    }
    const left_data = [];
    const right_data = [];
    data.left = data.left==''?[]:data.left.split(",").map(e=>+e)
    data.right =data.right==''?[]: data.right.split(",").map(e=>+e)
    data.left.forEach(n=>{
        if(n<19){
            left_data.push(`${n}-${n+18}`)
        }
    }) 
    data.right.forEach(n=>{
        if(n<19){
            right_data.push(`${n}-${n+18}`)
        }
    })
    
    const obj = {
        left_eye:{
            distance:data.distance,
            result: left_data,
        },
        right_eye:{
            distance:data.distance,
            result: right_data,
        }
    }
    return obj
}


/**
 * 转化第一次裸眼表单
 * @param {Array} data visionResultToData的结果数组
 * @returns 
 */
const visionResultsToInitalData = (data)=>{
    const obj = {
        left_eye:[],
        right_eye:[]
    }
    for(let i=5;i>0;i--){
        let item = null;
        for(let j=0;j<data.length;j++){
            if(data[j].left_eye.distance==i){
                item=data[j]
                continue;
            }
        }
        if(item!=null){
            obj.left_eye.push(item.left_eye)
            obj.right_eye.push(item.right_eye)
        }else{
            obj.left_eye.push({
                "vision": 0,
                "distance": i,
                "correct_num": 0
            })
            obj.right_eye.push({
                "vision": 0,
                "distance": i,
                "correct_num": 0
            })
        }
    }
    return obj;
}

/**
 * 预计购买的期数
 * （6  -  (裸眼能看到0.4的米数)   + （1.2和 聚光镜看到的视力单位的距离））* 2
 */
const getEstimatePayIssue=(testVsiionResults,condenserLensvisionResult)=>{
    const arr=testVsiionResults;
    const obj= condenserLensvisionResult;
    let issue = 0 
    console.log(arr);
    for(let i=0;i<arr.length;i++){
        const item  = arr[i];
        if(item.leftCompanyIndex<6){
            issue++
        }
        if(item.rightCompanyIndex<6){
            issue++
        }   
    }
    const index = Math.min(obj.leftCompanyIndex,obj.rightCompanyIndex);
    return Math.max(0,(22-index*2))+issue
}



export {
    visionResultToData,astigmatismResultToData,visionResultsToInitalData,getEstimatePayIssue
}